package com.bcu.team.comment.controller;

import com.bcu.team.comment.config.GlobalConfig;
import com.bcu.team.comment.entity.Message;
import com.bcu.team.comment.entity.Picture;
import com.bcu.team.comment.service.PictureService;
import com.bcu.team.comment.utils.LoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Api(tags = "图片模块")
@RestController
@RequestMapping("/api/picture")
public class PictureController {

    @Resource
    private PictureService pictureService ;
    @Resource
    private GlobalConfig globalConfig;

    @ApiOperation(value = "查询图片")
    @GetMapping(value = "/queryPicture")
    public Message queryUser(Integer pictureId) {
        return pictureService.queryPicture(pictureId);
    }

    @ApiOperation(value="上传图片")
    @PostMapping(value="/Images" , headers = "content-type=multipart/form-data")
    public String getImages(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        return globalConfig.hostUrl+"resource/"+ LoadUtil.upload(globalConfig.filePath,files, request);

    }

    @ApiOperation(value = "添加图片")
    @PutMapping(value = "/addPicture")
    public Message addUser(Picture picture){
        return pictureService.addPicture(picture);
    }


    @ApiOperation(value = "删除图片")
    @PutMapping(value = "/delPicture")
    public Message delUser(Integer pictureId){
        return pictureService.delPicture(pictureId);
    }

    @ApiOperation(value = "更新图片")
    @PutMapping(value = "/updatePicture")
    public Message updateUser(Picture picture){
        return pictureService.updatePicture(picture);
    }


}
